﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DSAManager.Core;
using DSAManager.Models.Input;
using DSAManager.Models.Output;
using DSAManager.Utils;

namespace DSAManager
{
    public partial class FrmUser : Form
    {
        public FrmUser()
        {
            InitializeComponent();
        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            resetInput();
            txt_name.Enabled = true;
        }

        void resetInput()
        {
            txt_name.Clear();
            txt_password.Clear();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            UserInfo info = new UserInfo();
            info.username = txt_name.Text;
            info.password = txt_password.Text;
            //info.width = Convert.ToSingle(num_width.Value);
            //info.heigth = Convert.ToSingle(num_heigth.Value);
            if (string.IsNullOrEmpty(info.username))
            {
                MsgBox.Alert("用户名不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(info.password) || info.password == "******")
            {
                MsgBox.Alert("密码不能为空或未发生更改!");
                return;
            }
            var result = Api.SaveUser(info);
            if (result.ok)
            {
                queryUser();
                MsgBox.Alert("保存成功!");
            }
            else
            {
                MsgBox.Alert("保存失败!");
            }
        }

        private void FrmUser_Load(object sender, EventArgs e)
        {
            queryUser();
        }

        List<UserInfo> userInfos = new List<UserInfo>();
        void queryUser()
        {

            BackgroundWorker bk = new BackgroundWorker();
            bk.DoWork += delegate
                             {
                                 userInfos.Clear();
                                 userInfos = Api.QueryUser().data;
                             };
            bk.RunWorkerCompleted += delegate
                                         {
                                             list_size.Items.Clear();
                                             foreach (var info in userInfos)
                                             {
                                                 ListViewItem item = list_size.Items.Add(info.username);
                                                 item.SubItems.Add(info.password.ToString());
                                                 //item.SubItems.Add(info.height.ToString());
                                                 item.Tag = info;
                                             }
                                             if (list_size.Items.Count > 0)
                                             {
                                                 list_size.Items[0].Selected = true;
                                                 list_size.Items[0].EnsureVisible();
                                                 //list_size.Items[0].
                                             }
                                         };
            bk.RunWorkerAsync();

        }

        private void list_size_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (list_size.SelectedItems.Count <= 0) return;
            var item = list_size.SelectedItems[0];

            var info = item.Tag as UserInfo;
            txt_name.Text = info.username;
            txt_password.Text = "******";
            txt_name.Enabled = false;
            //num_width.Value = Convert.ToDecimal(info.width);
            //num_heigth.Value = Convert.ToDecimal(info.height);

        }
    }
}
